<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Salary extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('Function_model');
        $this->load->model('Login_model');
        $this->load->model('Salary_model');
        $this->load->model('Interviewer_model');
        $this->load->model('Staff_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
        $this->load->library('pagination');
    }

    public function index()
    {
        $this->load->library('pagination');
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $dayform=trim($this->security->sanitize_filename($this->input->post('txtDayFrom')));
        $monthform=trim($this->security->sanitize_filename($this->input->post('txtMonthFrom')));
        $yearform=trim($this->security->sanitize_filename($this->input->post('txtYearFrom')));
        $fromdate=$yearform."-".$monthform."-".$dayform;
        $dayto=trim($this->security->sanitize_filename($this->input->post('txtDayTo')));
        $monthto=trim($this->security->sanitize_filename($this->input->post('txtMonthTo')));
        $yearto=trim($this->security->sanitize_filename($this->input->post('txtYearTo')));
        $todate=$yearto."-".$monthto."-".$dayto;
        
        if($fromdate=="--"||$todate=="--")
        {
        $fromdate=$this->uri->segment(3);
        $todate=$this->uri->segment(4);
        }
       
        $query=$this->Salary_model->loadListSalary($fromdate,$todate);
        
        foreach($query->result() as $row)
        {
            $this->Function_model->calculateWeekday($row->starttime);
            
        }
        $data['query2']=$this->Salary_model->loadListSalary($fromdate,$todate);
        $start_row = $this->uri->segment(5);
        $per_page = 10;
        $config['base_url'] =base_url().'salary/index/'.$fromdate.'/'.$todate;
        $data['fromdate']=$fromdate;
        $data['todate']=$todate;
        $config['total_rows'] = $query->num_rows();
        $data['total_rows'] = $query->num_rows();
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['title_content']="Manage Staff and Salary";
        $data['query'] = $this->Salary_model->loadListSalaryLimited($fromdate,$todate,$per_page,$start_row);
        //$data['query']=$this->Salary_model->loadListSalary($fromdate,$todate);
        $this->load->view('_header',$data);
        $this->load->view('salary/getResultSalary'); 
        $this->load->view('_footer');
        
        
    }
    public function loadSearchSalary()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->load->view('_header',$data);
        $this->load->view('salary/loadSearchSalary');
        $this->load->view('_footer');
    }
    
    public function viewSalaryStaff($Staff_ID)
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $query=$this->Staff_model->getFullNameByStaffID($Staff_ID);
        $query1=$this->Salary_model->getDatabaseIDbyStaffID($Staff_ID);
        $data['Staff_ID']=$Staff_ID;
        foreach($query->result() as $row)
        {
            $data['FullName']=$row->FullName;
        }
        foreach ($query1->result() as $row1)
        {
            $data['Database_ID']=$row1->Database_ID;
        }
        $this->load->view('_header',$data);
        $this->load->view('salary/loadSearchSalaryStaff'); 
        $this->load->view('_footer');
    }
    public function listSalaryStaff()
    {
        $this->load->library('pagination');
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $Database_ID=trim($this->security->sanitize_filename($this->input->post('txtDatabaseID')));
        $Staff_ID=trim($this->security->sanitize_filename($this->input->post('txtStaffID')));
        /*$query2=$this->Salary_model->getFullNameByID($Staff_ID);
        foreach($query2->result() as $row2)
        {
            $data['FullName']=$row2->FullName;
        }*/
        $dayform=trim($this->security->sanitize_filename($this->input->post('txtDayFrom')));
        $monthform=trim($this->security->sanitize_filename($this->input->post('txtMonthFrom')));
        $yearform=trim($this->security->sanitize_filename($this->input->post('txtYearFrom')));
        $fromdate=$yearform."-".$monthform."-".$dayform;
        $dayto=trim($this->security->sanitize_filename($this->input->post('txtDayTo')));
        $monthto=trim($this->security->sanitize_filename($this->input->post('txtMonthTo')));
        $yearto=trim($this->security->sanitize_filename($this->input->post('txtYearTo')));
        $todate=$yearto."-".$monthto."-".$dayto;
        
        if($fromdate=="--"||$todate=="--")
        {
        $fromdate=$this->uri->segment(3);
        $todate=$this->uri->segment(4);
        $Database_ID=$this->uri->segment(5);
        }
        $query=$this->Salary_model->loadListSalaryStaff($Database_ID,$fromdate,$todate);
        $data['query1']=$this->Salary_model->loadListSalaryStaff($Database_ID,$fromdate,$todate);
        //$data['query2']=getNumberDayStaff($Database_ID,$fromdate,$todate);
        $start_row = $this->uri->segment(6);
        $data['fromdate']=$fromdate;
        $data['todate']=$todate;
        $per_page = 10;
        $config['base_url'] =base_url().'salary/listSalaryStaff/'.$fromdate.'/'.$todate.'/'.$Database_ID;
        $config['total_rows'] = $query->num_rows();
        $data['total_rows'] = $query->num_rows();
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 6;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['title_content']="Manage Staff and Salary";
        $data['query'] = $this->Salary_model->loadListSalaryLimitedSalary($Database_ID,$fromdate,$todate,$per_page,$start_row);
        //$data['query']=$this->Salary_model->loadListSalary($fromdate,$todate);
        $this->load->view('_header',$data);
        $this->load->view('salary/getResultSalaryStaff'); 
        $this->load->view('_footer');
        
    }
    public function loadSearchWorkday()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->load->view('_header',$data);
        $this->load->view('salary/loadSearchWorkday');
        $this->load->view('_footer');
    }
    public function getListWorkday()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $dayform=trim($this->security->sanitize_filename($this->input->post('txtDayFrom')));
        $monthform=trim($this->security->sanitize_filename($this->input->post('txtMonthFrom')));
        $yearform=trim($this->security->sanitize_filename($this->input->post('txtYearFrom')));
        $fromdate=$yearform."-".$monthform."-".$dayform;
        $dayto=trim($this->security->sanitize_filename($this->input->post('txtDayTo')));
        $monthto=trim($this->security->sanitize_filename($this->input->post('txtMonthTo')));
        $yearto=trim($this->security->sanitize_filename($this->input->post('txtYearTo')));
        $todate=$yearto."-".$monthto."-".$dayto;
        
        if($fromdate=="--"||$todate=="--")
        {
        $fromdate=$this->uri->segment(3);
        $todate=$this->uri->segment(4);
        }
        $query=$this->Salary_model->getNumberDay($fromdate,$todate);
        foreach($query->result() as $row){
           $this->Function_model->insertTime($row->userid,$row->numberday,$fromdate,$todate);
        }
        $start_row = $this->uri->segment(5);
        $per_page = 10;
        $config['base_url'] =base_url().'salary/getListWorkday/'.$fromdate.'/'.$todate;
        $data['fromdate']=$fromdate;
        $data['todate']=$todate;
        $config['total_rows'] = $query->num_rows();
        $data['total_rows'] = $query->num_rows();
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['title_content']="Manage Staff and Salary";
        $data['query'] = $this->Salary_model->getNumberDayLimited($fromdate,$todate,$per_page,$start_row);
        //$data['query']=$this->Salary_model->loadListSalary($fromdate,$todate);
        $this->load->view('_header',$data);
        $this->load->view('salary/getResultWorkday'); 
        $this->load->view('_footer');
    }
    public function loadLabWorkday()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->load->view('_header',$data);
        $this->load->view('salary/loadLabWorkday');
        $this->load->view('_footer');
    }
    public function getAllWorkdayLab()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $dayform=trim($this->security->sanitize_filename($this->input->post('txtDayFrom')));
        $monthform=trim($this->security->sanitize_filename($this->input->post('txtMonthFrom')));
        $yearform=trim($this->security->sanitize_filename($this->input->post('txtYearFrom')));
        $fromdate=$yearform."-".$monthform."-".$dayform;
        $dayto=trim($this->security->sanitize_filename($this->input->post('txtDayTo')));
        $monthto=trim($this->security->sanitize_filename($this->input->post('txtMonthTo')));
        $yearto=trim($this->security->sanitize_filename($this->input->post('txtYearTo')));
        $todate=$yearto."-".$monthto."-".$dayto;
        $Lab_ID=trim($this->security->sanitize_filename($this->input->post('txtLabID')));
        /*if($fromdate=="--"||$todate=="--"||$Lab_ID==0)
        {
        $fromdate=$this->uri->segment(3);
        $todate=$this->uri->segment(4);
        $Lab_ID=$this->uri->segment(5);
        }
        $query=$this->Salary_model->getAllWorkDayLab($Lab_ID,$fromdate,$todate);
        $data['query0']=$this->Salary_model->getAllWorkDayLab($Lab_ID,$fromdate,$todate);
        $start_row = $this->uri->segment(6);
        $per_page = 10;
        $config['base_url'] =base_url().'salary/getAllWorkdayLab/'.$fromdate.'/'.$todate.'/'.$Lab_ID;
        $data['fromdate']=$fromdate;
        $data['todate']=$todate;
        $data['Lab_ID']=$Lab_ID;
        $config['total_rows'] = $query->num_rows();
        $data['total_rows'] = $query->num_rows();
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 6;
        $this->pagination->initialize($config);
        $data['pagination'] = $this->pagination->create_links();
        $data['title_content']="Manage Staff and Salary";
        $data['query'] = $this->Salary_model->getAllWorkDayLabLimited($Lab_ID,$fromdate,$todate,$per_page,$start_row);
        
        
        //$query=$this->Salary_model->getAllWorkDayLab($Lab_ID,$fromdate,$todate);
        //$data['query']=$this->Salary_model->getAllWorkDayLab($Lab_ID,$fromdate,$todate);
        //$data['query']=$this->Salary_model->getAllWorkDayLabString($Lab_ID,$fromdate,$todate);
        $data['total_rows']=$query->num_rows();*/
        $data['fromdate']=$fromdate;
        $data['todate']=$todate;
        $data['Lab_ID']=$Lab_ID;
        $data['query'] = $this->Salary_model->getAllWorkDayLabString($Lab_ID,$fromdate,$todate);
        $this->load->view('_header',$data);
        $this->load->view('salary/getResultLabWorkday'); 
        $this->load->view('_footer');
    }
}
?>
